<template>
  <el-main>
    <div class="el-toolbar">
      <div class="el-toolbar-body" style="justify-content: flex-start;">
        <!-- 打开一个新的网页导出数据字典       -->
        <a href="http://localhost:8202/admin/cmn/dict/exportData" target="_blank">
          <el-button type="primary" style="margin-bottom: 10px"><i class="fa fa-plus"/> 导出数据字典</el-button>
        </a>
        <el-button type="primary" @click="importData"><i class="fa fa-plus"/> 导入数据字典</el-button>
      </div>
    </div>
    <el-table
      :data="dictList"
      v-loading="listLoading"
      style="width: 70%"
      row-key="id"
      border
      lazy
      :load="getChildren"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column prop="name" label="名称" width="230" align="left"></el-table-column>
      <el-table-column prop="dictCode" label="编码" width="220"></el-table-column>
      <el-table-column prop="value" label="值" width="230" align="left"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
    </el-table>

    <!-- 文件上传提示框   -->
    <el-dialog title="上传文件" :visible.sync="dialogTableVisible" width="580px">
      <el-upload
        :on-success="onUploadSuccess"
        class="upload-demo"
        drag
        action="http://localhost:8202/admin/cmn/dict/importData" 
        align="center"
        multiple>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传xlsx文件，且不超过500kb</div>
      </el-upload>
    </el-dialog>
  </el-main>
</template>

<script>
import dict from '../../api/cmn/dict'
export default {
  name: 'dictList',
  data() {
    return {
      dictList: [],
      dialogTableVisible: false,
      listLoading:true
    }
  },
  created() {
    this.getDictList(1);
  },
  methods: {
    // 根据 id 获取数据字典列表
    getDictList(id) {
      dict.getDictList(id).then(
        response => {
          // console.log(response.data)
          this.dictList = response.data;
          this.listLoading = false;
        }
      ).catch(
        error => {
          // 提示
          this.$message({
            type: 'error',
            message: '获取数据字典失败!'
          });
        }
      )
    },

    // 获取子列表（element 调用）
    getChildren(tree, treeNode, resolve) {
      setTimeout(() => {
        dict.getDictList(tree.id).then(response => {
          resolve(response.data)
        })
      }, 500)
    },

    // 打开文件导入提示框
    importData() {
      this.dialogTableVisible = true;
    },

    // 文件上传成功后
    onUploadSuccess() {
      // 提示
      this.$message({
        type: 'success',
        message: '文件上传成功！'
      });
      // 关闭弹框
      this.dialogTableVisible = false;
      // 更新列表
      this.getDictList(1);
    }
  }
}
</script>

<style scoped>

</style>
